export $(xargs <.env)

build_dir=$(cd $(dirname ${BASH_SOURCE[0]}); pwd)

# 创建项目目录
if [ ! -d ${project_path} ]; then
    mkdir ${project_path}
fi


# git 拉取代码
git_cmd="git clone -b ${git_branch} http://${git_username}:${git_password}@${git_url} ${project_path}"
if [ "`ls -A ${project_path}`" = "" ]; then
    ${git_cmd}
fi


# .env修改
env=${project_path}/.env
if [ ! -f "${env}" ]; then
    cp ${project_path}/.env.server.example ${env}
fi

awk '{ sub(/APP_NAME=aix/,"APP_NAME='${app_name}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/APP_ENV=local/,"APP_ENV=local"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/APP_DEBUG=true/,"APP_DEBUG=true"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/APP_URL=http:\/\/91caiqing.com/,"APP_URL='${app_url}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/DEBUGBAR_ENABLED=true/,"DEBUGBAR_ENABLED=false"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/LARAVELS_USE_PERSISTENT=true/,"LARAVELS_USE_PERSISTENT=false"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/DB_HOST=mysql/,"DB_HOST='${mysql_host}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/DB_PORT=3306/,"DB_PORT='${mysql_port}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/DB_DATABASE=aixsys/,"DB_DATABASE='${mysql_database}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/DB_USERNAME=root/,"DB_USERNAME='${mysql_username}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/DB_PASSWORD=root/,"DB_PASSWORD='${mysql_password}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/REDIS_HOST=redis/,"REDIS_HOST=127.0.0.1"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/QUEUE_DRIVER=redis/,"QUEUE_DRIVER=sync"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/REDIS_PASSWORD=null/,"REDIS_PASSWORD='${redis_password}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/SESSION_DOMAIN=.91caiqing.com/,"SESSION_DOMAIN='${session_domain}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}
awk '{ sub(/ELASTICSEARCH_HOST=elasticsearch/,"ELASTICSEARCH_HOST='${es_host}'"); print $0 }' ${env} > ${project_path}/env.tmp && mv ${project_path}/env.tmp ${env}

cd ${project_path} && composer install -vvv

# artisan
php artisan aix:config
php artisan jwt:secret --force

chown www:www -R ${project_path}
chmod 777 -R ${project_path}/storage

# crontab
if [ ! -d /home/www ]; then
    mkdir /home/www
    chown www:www -R /home/www
fi

if [ "`cat /etc/crontab | grep ${project_path}`" = "" ]; then
   echo "* * * * * www /usr/local/php/bin/php ${project_path}/artisan schedule:run >> /dev/null 2>&1" >> /etc/crontab
   service crond restart
fi

cd ${build_dir}
vhost_conf="${nginx_conf_path}/vhost/${app_url}.conf"
if [ ! -f ${vhost_conf} ]; then
    cp ./nginx.conf "${vhost_conf}"
    awk '{ gsub(/app_url/,"'${app_url}'"); print $0 }' ${vhost_conf} > ./nginx.tmp && mv ./nginx.tmp "${vhost_conf}"
    awk '{ gsub(/project_path/,"'${project_path}'"); print $0 }' ${vhost_conf} > ./nginx.tmp && mv ./nginx.tmp "${vhost_conf}"
    awk '{ gsub(/nginx_conf_path/,"'${nginx_conf_path}'"); print $0 }' ${vhost_conf} > ./nginx.tmp && mv ./nginx.tmp "${vhost_conf}"
fi
nginx -s reload
